]> git.r.bdr.sh - rbdr/super-polarity/blame - Super Polarity/Actors/Actor.cs
I think bullets come out now.
[rbdr/super-polarity] / Super Polarity / Actors / Actor.cs
CommitLineData
f8aec187
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.Graphics;
8
9namespace SuperPolarity
10{
11 class Actor
12 {
2af83e98
BB
13 protected Game game;
14
38c7d3f9
BB
15 public List<Actor> Children;
16
f8aec187
BB
17 // Graphics / In-Game
18 protected Texture2D Texture;
19 protected Vector2 Origin;
20 public bool Active;
21
22 // Physical Properties
2af83e98 23 public Vector2 Position;
f8aec187
BB
24 protected Vector2 Velocity;
25 protected Vector2 Acceleration;
38c7d3f9 26 public float Angle;
f8aec187
BB
27
28 // Constraints / Behavior
29 protected float MaxVelocity;
30 protected float AccelerationRate;
31
32 public int Width
33 {
34 get { return Texture.Width; }
35 }
36
37 public int Height
38 {
39 get { return Texture.Height; }
40 }
41
2af83e98
BB
42 public Actor(Game newGame)
43 {
44 game = newGame;
45 }
46
47 public virtual void Initialize(Texture2D texture, Vector2 position)
f8aec187
BB
48 {
49 Texture = texture;
50 Position = position;
51 Active = true;
52
38c7d3f9
BB
53 Children = new List<Actor>();
54
f8aec187
BB
55 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
56 Velocity = new Vector2(0, 0);
57 Acceleration = new Vector2(0, 0);
58
59 MaxVelocity = 5;
60 AccelerationRate = 10;
61 }
62
63 public void AutoDeccelerate(GameTime gameTime)
64 {
65 if (Acceleration.X == 0 && Velocity.X > 0)
66 {
67 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
68 {
69 Velocity.X = 0;
70 Acceleration.X = 0;
71 }
72 else
73 {
74 Acceleration.X = -AccelerationRate;
75 }
76 }
77
78 if (Acceleration.X == 0 && Velocity.X < 0)
79 {
80 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
81 {
82 Velocity.X = 0;
83 Acceleration.X = 0;
84 }
85 else
86 {
87 Acceleration.X = AccelerationRate;
88 }
89 }
90
91 if (Acceleration.Y == 0 && Velocity.Y > 0)
92 {
93 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
94 {
95 Velocity.Y = 0;
96 Acceleration.Y = 0;
97 }
98 else
99 {
100 Acceleration.Y = -AccelerationRate;
101 }
102 }
103
104 if (Acceleration.Y == 0 && Velocity.Y < 0)
105 {
106 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
107 {
108 Velocity.Y = 0;
109 Acceleration.Y = 0;
110 }
111 else
112 {
113 Acceleration.Y = AccelerationRate;
114 }
115 }
116 }
117
118 public virtual void Update(GameTime gameTime)
119 {
120 Move(gameTime);
121 ChangeAngle();
38c7d3f9 122 CheckOutliers();
f8aec187
BB
123 }
124
38c7d3f9 125 public virtual void Move(GameTime gameTime)
f8aec187
BB
126 {
127 AutoDeccelerate(gameTime);
128
38c7d3f9
BB
129 var maxVelocity = MaxVelocity;
130
f8aec187
BB
131 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
132 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
133
134 if (Velocity.X > MaxVelocity)
135 {
136 Velocity.X = MaxVelocity;
137 }
138
139 if (Velocity.X < -MaxVelocity)
140 {
141 Velocity.X = -MaxVelocity;
142 }
143
144 if (Velocity.Y > MaxVelocity)
145 {
146 Velocity.Y = MaxVelocity;
147 }
148
149 if (Velocity.Y < -MaxVelocity)
150 {
151 Velocity.Y = -MaxVelocity;
152 }
153
154 Position.X = Position.X + Velocity.X;
155 Position.Y = Position.Y + Velocity.Y;
156 }
157
158 public void ChangeAngle()
159 {
160 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
161 }
162
163 public virtual void Draw(SpriteBatch spriteBatch)
164 {
38c7d3f9
BB
165 foreach (Actor child in Children)
166 {
167 child.Draw(spriteBatch);
168 }
169
f8aec187
BB
170 spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f);
171 }
38c7d3f9
BB
172
173 void CheckOutliers()
174 {
175 for (var i = Children.Count; i > 0; i--)
176 {
177 var actor = Children[i - 1];
178 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
179 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
180 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
181 {
182 Children.Remove(actor);
183 }
184 }
185 }
f8aec187
BB
186 }
187}